// 通过类的方式进行封装 
class WxRequest {
  constructor(params = {}) {
    this.default = Object.assign({}, this.default, params)
  }
  default = {
    baseURL: 'https://gmall-prod.atguigu.cn/mall-api',
    url: '',
    method: 'GET',
    header: {
      "Content-type": 'application/json', //设置数据的交互格式
      timeout: 60000
    }
  }
  // 定义拦截器对象
  interceptors = {
    response(config) {
      return config
    },
    request(res) {
      return res
    }
  }
  request(option) {
    console.log(this.default);
    option.url = this.default.baseURL + option.url
    option = {
      ...this.default,
      ...option
    }
    return new Promise((resolve, reject) => {
      wx.request({
        ...option,
        success: (res) => {
          resolve(res)
        },
        fail: (error) => {
          reject(error)
        }
      })
    })
  }
  get(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "GET"
    }, config))
  }
  delete(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "DELETE"
    }, config))
  }
  post(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "POST"
    }, config))
  }
  put(url, data = {}, config = {}) {
    return this.request(Object.assign({
      url,
      data,
      method: "PUT"
    }, config))
  }
}

const instanceRequest = new WxRequest({
  timeout: 150000
})
export default instanceRequest